package com.mkj.list;

import javax.swing.*;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;
import java.awt.*;
import java.util.List;
import java.util.Vector;

/**
 * @Author Arrebol
 * @Date 2024/8/7 16:19
 * @Project graphical programming
 * @Description：
 * 简单列表框组件基本演示
 */
public class ListTest {

    JFrame frame = new JFrame("列表框测试");
    String[] books={"Java自学宝典","轻量级JavaEE企业级开发","Android基础教程","jQuery实战教程","SpringBoot企业级开发"};
    //声明布局选择按钮所在的面板
    JPanel layoutPanel = new JPanel();
    ButtonGroup layoutGroup = new ButtonGroup();
    //定义选择模式按钮所在的面板
    JPanel selectModelPanel = new JPanel();
    ButtonGroup selectModelGroup = new ButtonGroup();
    JTextArea favorite = new JTextArea(4,40);
    //使用字符串数组创建JList对象
    JList<String> bookList;
    JComboBox<String> bookSelector;




    public void init(){
        //组装视图
        //组装JList
        bookList = new JList<>(books);
        addButtonToLayoutPanel("纵向滚动",JList.VERTICAL);
        addButtonToLayoutPanel("纵向换行",JList.VERTICAL_WRAP);
        addButtonToLayoutPanel("横向换行",JList.HORIZONTAL_WRAP);
        addButtonToSelectModePanel("无限制",ListSelectionModel.SINGLE_INTERVAL_SELECTION);
        addButtonToSelectModePanel("单选",ListSelectionModel.SINGLE_SELECTION);
        addButtonToSelectModePanel("单范围",ListSelectionModel.SINGLE_INTERVAL_SELECTION);
        //对JList做设置
        bookList.setVisibleRowCount(3);
        bookList.setSelectionInterval(2,4);
        //处理条目选中事件
        bookList.addListSelectionListener(e -> {
            //获取当前选中的条目
            List<String> selectedValuesList = bookList.getSelectedValuesList();
            //将当前选中条目的内容设置到文本域中
            for (String s : selectedValuesList) {
                favorite.append(s+"\n");
            }
        });
        Box bookListVBox = Box.createVerticalBox();
        bookListVBox.add(new JScrollPane(bookList));
        bookListVBox.add(layoutPanel);
        bookListVBox.add(selectModelPanel);
        //组装JComboBox
        Vector<String> vector = new Vector<>();
        List<String> list = List.of("Java自学宝典", "轻量级JavaEE企业级开发", "Android基础教程", "jQuery实战教程", "SpringBoot企业级开发");
        vector.addAll(list);
        bookSelector = new JComboBox<>(new Vector<>(vector));
        bookSelector.setEditable(true);
        bookSelector.setMaximumRowCount(4);
        bookSelector.addItemListener(e -> {
            Object selectedItem = bookSelector.getSelectedItem();
            assert selectedItem != null;
            favorite.setText(selectedItem.toString());
        });
        //组装顶部的两部分
        Box topHBox = Box.createHorizontalBox();
        topHBox.add(bookListVBox);
        JPanel bookSelectPanel = new JPanel();
        bookSelectPanel.add(bookSelector);
        topHBox.add(bookSelectPanel);
        //组装底部
        JPanel bottomPanel = new JPanel();
        bottomPanel.setLayout(new BorderLayout());
        bottomPanel.add(new JLabel("您最喜欢的图书："),BorderLayout.NORTH);
        bottomPanel.add(favorite);
        //组装整体
        Box wholeBox = Box.createVerticalBox();
        wholeBox.add(topHBox);
        wholeBox.add(bottomPanel);
        frame.add(wholeBox);
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    }

    /**
     * 向LayoutPanel中添加按钮
     * @param name
     * @param layoutType
     */
    public void addButtonToLayoutPanel(String name,int layoutType){
        //设置标题边框
        layoutPanel.setBorder(new TitledBorder(new EtchedBorder(),"确定选项布局"));
        //创建单选按钮
        JRadioButton button = new JRadioButton(name);
        layoutPanel.add(button);
        //让第一个按钮默认选中
        if(layoutGroup.getButtonCount()==0){
            button.setSelected(true);
        }
        layoutGroup.add(button);
        button.addActionListener(e -> {
            bookList.setLayoutOrientation(layoutType);
        });
    }

    /**
     * 向selectModel中添加按钮
     * @param name
     * @param selectionMode
     */
    public void addButtonToSelectModePanel(String name,int selectionMode){
        //设置标题边框
        selectModelPanel.setBorder(new TitledBorder(new EtchedBorder(),"确定选择模式"));
        //创建单选按钮
        JRadioButton button = new JRadioButton(name);
        selectModelPanel.add(button);
        //让第一个按钮默认选中
        if(selectModelGroup.getButtonCount()==0){
            button.setSelected(true);
        }
        selectModelGroup.add(button);
        button.addActionListener(e -> {
            bookList.setSelectionMode(selectionMode);
        });
    }

    public static void main(String[] args) {
        new ListTest().init();
    }
}
